package com.kingyun.gpsinspection.purificationservice.services.jsm.innerservice.impl.entitytype;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cqhilink.api.common.utils.UniqueUtil;
import com.cqhilink.api.common.utils.date.DateStyle;
import com.cqhilink.api.common.utils.date.DateUtil;
import com.kingyun.gpsinspection.purificationservice.dao.mapper.jsm.EntityTypePropertyMapper;
import com.kingyun.gpsinspection.purificationservice.facade.model.jsm.Employee;
import com.kingyun.gpsinspection.purificationservice.facade.model.jsm.EntityTypeProperty;
import com.kingyun.gpsinspection.purificationservice.facade.service.jsm.entitytype.JsmEntityTypePropertyService;
import com.google.common.collect.Lists;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.math.BigDecimal;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * @Author 卢曼成
 * @Description 实体类特性值Service实现
 * @Create 2017-02-23 15:28
 */

public class JsmEntityTypePropertyServiceImpl implements JsmEntityTypePropertyService {
    @Autowired
    private EntityTypePropertyMapper entityTypePropertyMapper;

    /**
     * @param devicePropertyId,实体特性值唯一标识
     */
    @Override
    public int deleteByPrimaryKey(String devicePropertyId) {
        return entityTypePropertyMapper.deleteByPrimaryKey(devicePropertyId);
    }

    /**
     * @param propertyIds 批量删除
     * @return
     */
    @Override
    public int deleteByDevicePropertyIds(Map propertyIds) {
        return entityTypePropertyMapper.deleteByDevicePropertyIds(propertyIds);
    }

    /**
     * @param record 生产生产实体类型特性值-插入
     * @return
     */
    @Override
    public int insert(EntityTypeProperty record) {
        return entityTypePropertyMapper.insert(record);
    }

    /**
     * @param record 生产生产实体类型特性值-插入
     * @return
     */
    @Override
    public int insertSelective(EntityTypeProperty record) {
        return entityTypePropertyMapper.insertSelective(record);
    }

    /**
     * @param devicePropertyId 根据生产实体ID
     * @return
     */
    @Override
    public EntityTypeProperty selectByPrimaryKey(String devicePropertyId) {
        return entityTypePropertyMapper.selectByPrimaryKey(devicePropertyId);
    }

    /**
     * @param record 根据生产实体特性值-修改
     * @return
     */
    @Override
    public int updateByPrimaryKeySelective(EntityTypeProperty record) {
        return entityTypePropertyMapper.updateByPrimaryKeySelective(record);
    }

    /**
     * @param record 根据生产实体特性值-修改
     * @return
     */
    @Override
    public int updateByPrimaryKey(EntityTypeProperty record) {
        return entityTypePropertyMapper.updateByPrimaryKey(record);
    }

    /**
     * @param entityTypeProperty 查询分页数量
     * @return
     */
    @Override
    public List<EntityTypeProperty> queryTableList(EntityTypeProperty entityTypeProperty) {
        return entityTypePropertyMapper.queryTableList(entityTypeProperty);
    }

    /**
     * @param entityTypeId
     * @return 根据生产生产实体类型ID查询特性值
     */
    @Override
    public List<EntityTypeProperty> outQueryList(String entityTypeId) {
        return entityTypePropertyMapper.outQueryList(entityTypeId);
    }

    /**
     * @Author 卢曼成
     * @Description 批量插入
     * @Date 2017/2/21 16:48
     */
    @Override
    public int insertList(List<EntityTypeProperty> list) {
        return entityTypePropertyMapper.insertList(list);
    }

    /**
     * @Author 卢曼成
     * @Description 批量逻辑删除
     * @Date 2017/2/21 17:10
     */
    @Override
    public int deleteList(List<EntityTypeProperty> list) {
        return entityTypePropertyMapper.deleteList(list);
    }

    /**
     * @Author 卢曼成
     * @Description 根据EntityId查询所有信息
     * @Date 2017/2/21 17:03
     */
    @Override
    public List<EntityTypeProperty> selectByParentKey(EntityTypeProperty param) {
        return entityTypePropertyMapper.selectByParentKey(param);
    }

    /**
     * @Author 卢曼成
     * @Description 根据父级ID删除
     * @Date 2017/2/22 11:00
     */
    @Override
    public int deleteParentKey(EntityTypeProperty param) {
        return entityTypePropertyMapper.deleteParentKey(param);
    }

    /**
     * @Author 卢曼成
     * @Description 批量处理实体特性值
     * @Date 2017/2/23 15:36
     */
//    @Override
//    public int batchEntityTypeProperty(String strList, String entityTypeId, String userId, Date date, int isNum) {
//        if (StringUtils.isNotEmpty(strList)) {
//            //处理EntityTypeProperty
//            List<EntityTypeProperty> etpList = JSON.parseArray(strList, EntityTypeProperty.class);
//            if (isNum == 1) {
//                for (EntityTypeProperty param : etpList) {
//                    entityTypeProperty(param, entityTypeId, userId, date);
//                }
//            } else if (isNum == 2) {
//                //处理EntityTypeProperty
//                EntityTypeProperty param = new EntityTypeProperty();
//                param.setEntityTypeId(entityTypeId);
//                param.setStatus(BigDecimal.valueOf(0));
//                List<EntityTypeProperty> deleteList = this.selectByParentKey(param);
//                Iterator<EntityTypeProperty> it = etpList.iterator();
//                while (it.hasNext()) {
//                    EntityTypeProperty etp = it.next();
//                    if (StringUtils.isNotEmpty(etp.getDevicePropertyId())) {
//                        boolean is = contrast(deleteList, etp.getDevicePropertyId());
//                        if (is) {
//                            it.remove();
//                        }
//                    } else {
//                        entityTypeProperty(etp, entityTypeId, userId, date);
//                    }
//                }
//                if (deleteList != null && deleteList.size() > 0) {
//                    this.deleteList(deleteList);
//                }
//            }
//            if (etpList.size() > 0) {
//                this.insertList(etpList);
//            }
//        }
//        return 0;
//    }

    /**
     * 特性值新增-插入
     *
     * @param etp          实体特性值json
     * @param entityTypeId 生产实体类型
     */
    @Override
    public int batchEntityTypeProperty(String etp, String entityTypeId, Employee employee) throws Exception {
        int resInt = 0;
        EntityTypeProperty entityTypeProperty = JSON.parseObject(etp, EntityTypeProperty.class);
        if (entityTypeProperty != null) {
            entityTypeProperty.setEntityTypeId(entityTypeId);
            resInt = addEntityTypeProperty(entityTypeProperty, employee);
        }
        return resInt;
    }

    /**
     * @Author 卢曼成
     * @Description 判断是否存在
     * @Date 2017/2/21 17:27
     */
    private boolean contrast(List<EntityTypeProperty> jList, String devicePropertyId) {
        boolean is = false;
        Iterator<EntityTypeProperty> it = jList.iterator();
        while (it.hasNext()) {
            EntityTypeProperty param = it.next();
            if (param.getDevicePropertyId().equals(devicePropertyId)) {
                is = true;
                it.remove();
            }
        }
        return is;
    }

    /**
     * @Author 卢曼成
     * @Description 租转EntityTypeProperty实体数据
     * @Date 2017/2/22 8:41
     */
    private void entityTypeProperty(EntityTypeProperty param, String id, String userId, Date date) {
        param.setDevicePropertyId(UniqueUtil.uuid());
        param.setEntityTypeId(id);
        param.setModifyer(userId);
        param.setModifyTime(DateUtil.DateToString(date, DateStyle.YYYY_MM_DD_HH_MM));
        param.setCreateTime(DateUtil.DateToString(date, DateStyle.YYYY_MM_DD_HH_MM));
        param.setCreator(userId);
        param.setStatus(BigDecimal.valueOf(0));
    }

    /**
     * @param entityTypeProperty 实体特性值
     * @table 获取特性值
     */
    @Override
    public JSONObject table(EntityTypeProperty entityTypeProperty) throws Exception {
        JSONObject jsonObject = new JSONObject();
        entityTypeProperty.setOffset(0);
        entityTypeProperty.setLimit(Integer.MAX_VALUE);
        List<EntityTypeProperty> entityTypeProperties = entityTypePropertyMapper.queryTableList(entityTypeProperty);
        if (entityTypeProperties == null || entityTypeProperties.size() == 0) {
            jsonObject.put("total", 0);
            jsonObject.put("rows", Lists.newArrayList());
        } else {
            jsonObject.put("total", entityTypeProperties.size());
            jsonObject.put("rows", JSON.toJSON(entityTypeProperties));
        }
        return jsonObject;
    }

    @Override
    public int addEntityTypeProperty(EntityTypeProperty entityTypeProperty, Employee employee) throws Exception {
        if (StringUtils.isNotEmpty(entityTypeProperty.getDevicePropertyId())) {
            entityTypeProperty.setModifyTime(DateUtil.DateToString(new Date(), DateStyle.YYYY_MM_DD_HH_MM));
            entityTypeProperty.setModifyer(employee.getEmployeeId());
            return entityTypePropertyMapper.updateByPrimaryKeySelective(entityTypeProperty);
        } else {
            entityTypeProperty.setDevicePropertyId(UniqueUtil.uuid());
            entityTypeProperty.setCreateTime(DateUtil.DateToString(new Date(), DateStyle.YYYY_MM_DD_HH_MM));
            entityTypeProperty.setCreator(employee.getEmployeeId());
            entityTypeProperty.setModifyTime(DateUtil.DateToString(new Date(), DateStyle.YYYY_MM_DD_HH_MM));
            entityTypeProperty.setModifyer(employee.getEmployeeId());
            entityTypeProperty.setStatus(BigDecimal.valueOf(0));
            return entityTypePropertyMapper.insertSelective(entityTypeProperty);
        }

    }


    /**
     * 删除生产实体类型-特性
     *
     * @param propertyIds
     */
    @Override
    public Integer delEntityTypeProperty(Map propertyIds) throws Exception {

        return entityTypePropertyMapper.deleteByDevicePropertyIds(propertyIds);
    }
}
